/**
 * @(#) HospitalProxy.as
 *
 * @date		2009. 05. 07.
 * @version		1.0
 * @author		Park, Juneho
 * @description	 건강검진 Proxy - Proxy
 * 				건강검진 조회를한다.
 */
package com.skt.common_hr.healthcheckup.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.puremvc.DataReceiver;
	import com.skt.common_hr.healthcheckup.HealthCheckUpFacade;
	import com.skt.common_hr.healthcheckup.model.vo.HospitalDefaultListVO;
	import com.skt.common_hr.healthcheckup.model.vo.HospitalDefaultVO;
	import com.skt.common_hr.healthcheckup.model.vo.HospitalVO;
	
	import mx.collections.ArrayCollection;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class HospitalProxy extends CommonProxy
	{
		public static const NAME:String = "HospitalProxy";
		
		public var hospitalList:ArrayCollection;
		public var hospitalDetail:ArrayCollection;
		public var reservableMonthList:ArrayCollection;
		public var reservableDate:ArrayCollection;
		public var attached_url:String;
		
		public var selectableItems:ArrayCollection;
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function HospitalProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		
		//병원목록
		public function loadList():void
		{
			var params:Object = new Object();
			loadData("Z_HR_0090", params, listResult, listFault);
		}
		
		//병원상세정보
		public function loadDetail(code:String):void
		{
			var params:Object = new Object();
			params["I_STCD2"] = code;
			
			loadData("Z_HR_0091", params, detailResult, detailFault);
		}
		
		
		//선택진료항목
		public function loadSelectableItems(code:String):void
		{
			
			var params:Object = new Object();
			params["I_STCD2"] = code;
			
			loadMultiData("Z_HR_0092", params, choiceResult);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function listResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				hospitalList = parseHospitalList(event.result.ITAB);
				facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to load cong: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_LIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function listFault(event:FaultEvent):void
		{
			trace("Fault message in HealthCheckUpProxy: " + event.fault.faultString);
			facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_LIST_FAILED, this);
		}
		
	
		public function detailResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				hospitalDetail = parseHospitalDetail(event.result.ITAB1);
				reservableDate = parseReservableDate(event.result.ITAB2);
				reservableMonthList = parseReservableMonth(reservableDate);
				attached_url = event.result["O_URL"];
				facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_DEFAULT_COMPLETE, this);
			}
			else {
				trace( "failed to load cong: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_DEFAULT_FAILED, this);
			}
		}
		
		private function detailFault(event:FaultEvent):void
		{
			facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_DEFAULT_FAILED, this);
		}
		
		
		public function choiceResult(event:DataReceiver):void
		{
			if(event.retCode==0) {  
				selectableItems = parseChoiceCombo(event.result.ITAB);
				facade.sendNotification(HealthCheckUpFacade.LOAD_CHOICE_COMBO_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to load cong: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(HealthCheckUpFacade.LOAD_CHOICE_COMBO_LIST_FAILED, this);
			}
		}
		
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		private function parseHospitalList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr)
			{
				var vo:HospitalVO = new HospitalVO();
				
				vo.STCD2	= rec["STCD2"];
				vo.COMNM	= rec["COMNM"];
				retArr.addItem(vo);
			}
			
			return retArr;
		}
		
		private function parseHospitalDetail(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr)
			{
				var vo:HospitalDefaultVO = new HospitalDefaultVO();
				
				vo.STCD2	= rec["STCD2"];
				vo.COMNM	= rec["COMNM"];
				
				vo.STCD2		= rec["STCD2"];			//병원
				vo.COMNM		= rec["COMNM"];			//업체명
				vo.ZIPCO		= rec["ZIPCO"];			//우편번호
				vo.ADDR			= rec["ADDR"];			//주소
				vo.TELNM		= rec["TELNM"];			//대표자 Office Tel.
				vo.NOTICE		= rec["NOTICE"];		//특징
				vo.HOME_URL		= rec["HOME_URL"];		//TEXT
				
				retArr.addItem(vo);
			}
			
			return retArr;
		}
		
		private function parseReservableDate(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			reservableMonthList = new ArrayCollection();
			var month:int;
			
			for each(var rec:Object in srcArr)
			{
				var vo:HospitalDefaultListVO = new HospitalDefaultListVO();
				
				vo.MONTH		= rec["MONTH"];			//월
				//vo.REDAT		= DateUtil.toDefaultString(rec["REDAT"]);			//예약가능일
				vo.REDAT		= rec["REDAT"];			//예약가능일
				vo.RECNT		= rec["RECNT"];			//예약가능 인원수
				vo.RECNT2		= rec["RECNT2"];		//예약인원수
				vo.RECNT3		= rec["RECNT3"];		//잔여인원수
				
				retArr.addItem(vo);
			}
			
			return retArr;
		}
		
		private function parseReservableMonth(src:ArrayCollection):ArrayCollection
		{
			var monthList:ArrayCollection = new ArrayCollection();
			monthList.addItem({label:ResourceManager.getInstance().getString("HealthCheckUp", "SELECT"), data:0});
			for each(var vo:HospitalDefaultListVO in src) {
				if( isNewMonth(vo, monthList) ) {
					monthList.addItem({label:vo.MONTH, data:vo.MONTH});
				}
			}
			return monthList;
		}
		
		private function isNewMonth(vo:HospitalDefaultListVO, list:ArrayCollection):Boolean
		{
			for each(var month:Object in list) {
				if(month.data == vo.MONTH) {
					return false;
				}
			}
			return true;
		}

		private function parseChoiceCombo(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			retArr.addItem({label:ResourceManager.getInstance().getString("HealthCheckUp", "SELECT"), data:0});
			
			for each(var rec:Object in srcArr) 
			{
				//선택진료항목, 선택진료항목명
				retArr.addItem({label:rec["MEDTX"], data:rec["MEDCD"]});
			}
			return retArr;
		}
	}
}